// pages/record/record.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    select: false,
    breakfastselect: false,
    lunchselect: false,
    dinnerselect: false,
    additionalselect: false,

    grade_name: '--选择日期--',
    grades: ['2022-11-09', '2022-11-10', '2022-11-11', '2022-11-12'],

    breakfast: '早餐',
    breakfastgrades: ['早餐1，早餐2'],
    lunchgrades: ['午餐1，午餐2'],
    dinnergrades: ['晚餐1，晚餐2'],
    additionalgrades: ['加餐1，加餐2'],

    lunch: '午餐',
    supper: '晚餐',
    meal: '加餐',

    date: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate()
    // data: []
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
    // console.log(this.data.date)
    let data = this.data.data
    console.log('记录的数据：', data)
    for (var item in data) {
      let str1 = (data[item].record_date).substring(0, 9) 
      let str2 = (data[item].record_date).substring(9,10)
      let dateM = str1 + (parseInt(str2)+1)
      console.log('记录的时间：', dateM)
      console.log(this.data.date)
      //判断记录的时间与所选的时间是否相同  
      if (dateM == this.data.date) {
        //加餐
        if (data[item].record_meal == 4) {
          wx.request({
            method: "GET",
            url: 'http://127.0.0.1:8080/recipes/menu/' + data[item].menu_id,
            success: res => {
              // console.log(res.data[0]);
              this.setData({
                recipe_name: res.data[0].menu_name,
                recipe_energy: res.data[0].menu_car + res.data[0].menu_fat + res.data[0].menu_pr,
              })
              // console.log(this.data.recipe_name)
              let name = this.data.recipe_name
              // console.log(name)
              // this.data.additionalgrades.push(data[item].this.data.recipe_name)
              this.data.additionalgrades.push(name)
              this.setData({
                additionalgrades: this.data.additionalgrades
              })
            }
          })
          // console.log(this.data.additionalgrades)
        }
        //午餐
        else if (data[item].record_id == 3) {
          wx.request({
            method: "GET",
            url: 'http://127.0.0.1:8080/recipes/menu/' + 19,
            success: res => {
              // console.log(res.data[0]);
              this.setData({
                recipe_name: res.data[0].menu_name,
                recipe_energy: res.data[0].menu_car + res.data[0].menu_fat + res.data[0].menu_pr,
              })
              console.log(this.data.recipe_name)
              let name = this.data.recipe_name
              console.log(name)
              // this.data.additionalgrades.push(data[item].this.data.recipe_name)
              this.data.dinnergrades.push(data[item].menu_id)
              this.setData({
                additionalgrades: this.data.dinnergrades
              })
            }
          })
        } else if (data[item].record_id == 2) {
          wx.request({
            method: "GET",
            url: 'http://127.0.0.1:8080/recipes/menu/' + 19,
            success: res => {
              // console.log(res.data[0]);
              this.setData({
                recipe_name: res.data[0].menu_name,
                recipe_energy: res.data[0].menu_car + res.data[0].menu_fat + res.data[0].menu_pr,
              })
              console.log(this.data.recipe_name)
              let name = this.data.recipe_name
              console.log(name)
              // this.data.additionalgrades.push(data[item].this.data.recipe_name)
              this.data.lunchgrades.push(data[item].menu_id)
              this.setData({
                additionalgrades: this.data.lunch
              })
            }
          })

        } else if (data[item].record_id == 1) {
          wx.request({
            method: "GET",
            url: 'http://127.0.0.1:8080/recipes/menu/' + 19,
            success: res => {
              // console.log(res.data[0]);
              this.setData({
                recipe_name: res.data[0].menu_name,
                recipe_energy: res.data[0].menu_car + res.data[0].menu_fat + res.data[0].menu_pr,
              })
              console.log(this.data.recipe_name)
              let name = this.data.recipe_name
              console.log(name)
              // this.data.additionalgrades.push(data[item].this.data.recipe_name)
              this.data.breakfastgrades.push(data[item].menu_id)
              this.setData({
                additionalgrades: this.data.breakfastgrades
              })
            }
          })
        } else {
          this.setData({
            additionalgrades: this.data.breakfastgrades,
          })
        }
      } else {
        this.setData({
          additionalgrades: this.data.breakfastgrades,
        })
      }
    }
  },
  // 下拉菜单是否收回
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  // 早餐下拉菜单是否收回
  breakfast() {
    this.setData({
      breakfastselect: !this.data.breakfastselect
    })
  },
  lunch() {
    this.setData({
      lunchselect: !this.data.lunchselect
    })
  },
  dinner() {
    this.setData({
      dinnerselect: !this.data.dinnerselect
    })
  },
  additional() {
    this.setData({
      additionalselect: !this.data.additionalselect
    })
  },

  // record:function(){
  //   // var that = this;
  //   // var record_id = wx.getStorageSync('record_id');
  //   this.data.userid = wx.getStorageSync('userid')
  //   wx.request({
  //       //请求链接
  //       url: 'http://127.0.0.1:8080/record/',
  //       //发送的数据
  //       data: {
  //         record_id:record_id 
  //       },
  //       //成功回调
  //       success: function (res) {
  //           that.setData({
  //               record_date: res.data.data.record_date,
  //               record_meal: res.data.data.record_meal
  //           })
  //       },
  //   })
  // },

  // 获取当前选择的日期更新数据
  mySelect(e) {
    console.log(e)
    var name = e.currentTarget.dataset.name
    this.setData({
      grade_name: name,
      select: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.userid = wx.getStorageSync('userid')
    wx.request({
      //请求链接
      url: `http://127.0.0.1:8080/record/${this.data.userid}`,
      //成功回调
      success: res => {
        console.log(res.data)
        this.setData({
          data: res.data
        })
        // console.log(this.data.date)
      },
    })

    // wx.request({
    //   method:"GET",
    //   url: 'http://127.0.0.1:8080/recipes/menu/'+options.menu_id,
    //   success:res=>{
    //     // console.log(res.data[0]);
    //     this.setData({
    //       recipe:res.data[0]
    //     })
    //     console.log(this.data.recipe)
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // console.log(this.data.additionalgrades) 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data.additionalgrades)
    let data = this.data.data
    // let dateM = this.data.date
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})